package com.example.campustalent.activity

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.text.method.HideReturnsTransformationMethod
import android.text.method.PasswordTransformationMethod
import android.view.View
import android.widget.CompoundButton
import com.example.campustalent.R
import com.example.campustalent.bean.UserInfoBean
import com.example.campustalent.databinding.ActivityRegisterBinding
import com.example.campustalent.utils.DbUtils
import com.luck.picture.lib.tools.ToastUtils

class RegisterActivity : AppCompatActivity() {
    private lateinit var binding: ActivityRegisterBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityRegisterBinding.inflate(layoutInflater)
        val rootView: View = binding.root
        setContentView(rootView)
        if (DbUtils.liteOrm == null) {
            DbUtils.createDb(this, "CampusTalent")
        }
        binding.etName.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
            override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {}
            override fun afterTextChanged(s: Editable) {
                if (!TextUtils.isEmpty(s.toString())) {
                    binding.ivDelete.visibility = View.VISIBLE
                } else {
                    binding.ivDelete.visibility = View.GONE
                }
            }
        })

        binding.etPwd.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
            override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {}
            override fun afterTextChanged(s: Editable) {
                if (!TextUtils.isEmpty(s.toString())) {
                    binding.cbPwd.visibility = View.VISIBLE
                } else {
                    binding.cbPwd.visibility = View.GONE
                }
            }
        })

        binding.ivDelete.setOnClickListener {
            binding.etName.setText("")
        }
        binding.cbPwd.setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener { buttonView, isChecked ->
            binding.cbPwd.isChecked = isChecked
            if (isChecked) {
                binding.cbPwd.transformationMethod = HideReturnsTransformationMethod.getInstance()
            } else {
                binding.cbPwd.transformationMethod = PasswordTransformationMethod.getInstance()
            }
            binding.etPwd.setSelection(binding.etPwd.text.toString().length)
        })
        binding.tvRegister.setOnClickListener {
            if (binding.etName.text.toString().isEmpty() || binding.etPwd.text.toString().isEmpty()){
                ToastUtils.s(this, "用户名或密码不能为空")
                return@setOnClickListener
            }
            val list = DbUtils.getQueryAll(UserInfoBean::class.java)
            if (!list.isNullOrEmpty()){
                for (bean in list){
                    val list = DbUtils.getQueryByWhere(UserInfoBean::class.java, "name", arrayOf(binding.etName.text.toString()))
                    if (list.isNullOrEmpty()){
                        val userInfoBean = UserInfoBean()
                        userInfoBean.name = binding.etName.text.toString()
                        userInfoBean.password = binding.etPwd.text.toString()
                        userInfoBean.isFirst = true
                        DbUtils.insert(userInfoBean)
                        ToastUtils.s(this, "注册成功")
                        finish()
                        return@setOnClickListener
                    }else {
                        val userInfoBean = list[0]
                        if (binding.etName.text.toString() == userInfoBean.name){
                            ToastUtils.s(this, "用户已被注册")
                            return@setOnClickListener
                        }
                    }
                }
            }
            val userInfoBean = UserInfoBean()
            userInfoBean.name = binding.etName.text.toString()
            userInfoBean.password = binding.etPwd.text.toString()
            userInfoBean.isFirst = true
            DbUtils.insert(userInfoBean)
            ToastUtils.s(this, "注册成功")
            finish()
        }

        binding.ivBack.setOnClickListener {
            finish()
        }
    }
}